<?php

namespace ticky;

/**
 *  view.php 视图工厂类
 *
 * @author           罗敏贵
 * @license          https://www.cnblogs.com/luomingui
 * @lastmodify       2019-02-20
 */
class view {

    public static $instance = null;
    public static $driver = null;
    public static $config = null;
    public static $theme = 'default';

    private static function getconfig() {
        self::$theme = config::get('view', 'default_theme');
        self::$driver = config::get('view', 'driver');
        self::$config = config::get('view', self::$driver);
    }

    /**
     * 返回当前终级类对象的实例
     * @return object
     */
    public static function instance($name = false) {
        self::getconfig();
        $type = !empty(self::$driver) ? self::$driver : 'smarty';
        if (false === $name) {
            $name = md5(serialize(self::$config));
        }
        if (true === $name || !isset(self::$instance[$name])) {
            $class = false === strpos($type, '\\') ? '\\ticky\\view\\driver\\' . $type : $type;
            if (true === $name) {
                return new $class(self::$config);
            }
            self::$instance[$name] = new $class(self::$config);
        }
        return self::$instance[$name];
    }

}
